package com.firstDay;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class List_Ask {
    public static void main(String[] args) {

        List list = new ArrayList();
        list.add(new Book("红楼梦", "曹雪芹", null));
        list.add(new Book("西游记", "吴承恩", 10.0));
        list.add(new Book("水浒传", "施耐庵", 9.0));
        list.add(new Book("三国演义", "罗贯中", 80.0));

        ArrayList<Book> bookList = new ArrayList<>();
        bookList.addAll(list);

        // 使用stream流对价格进行升序排序
        System.out.println("===============升序排序===============");
        List<Book> list1 = bookList.stream()
                .sorted(Comparator.comparing(Book::getPrice, Comparator.nullsLast(Double::compare)))
                .collect(Collectors.toList());
        for (Book book : list1) {
            System.out.println(book);
        }

        System.out.println("===============降序排序===============");
        // 使用stream流对价格进行降序排序
        List<Book> list2 = bookList.stream()
                .sorted(Comparator.comparing(Book::getPrice, Comparator.nullsLast(Double::compare)).reversed())
                .collect(Collectors.toList());
        for (Book book : list2) {
            System.out.println(book);
        }

    }
}
